package com.mspbots.core.ticketai;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.ticketai.model.Subdata;
import com.mspbots.core.ticketai.service.SubDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Callable;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("tenant/subData")
public class SubDataController extends AbstractBaseController<Subdata, SubDataService> {

    @Override
    @GetMapping(path = "/list")
    public Callable selectList(Subdata queryParams) {
        QueryWrapper<Subdata> queryWrapper = new QueryWrapper<>(queryParams);
        return () -> {
            if ("COMPANY-STATUS".equalsIgnoreCase(queryParams.getType())) {
                return service.list(queryWrapper.select("cw_id", "name", "extend"))
                        .stream().filter(subdata -> !ObjectUtils.isEmpty(subdata.getExtend().get("inactiveFlag"))
                                && !subdata.getExtend().get("inactiveFlag").asBoolean())
                        .peek(subdata -> subdata.setExtend(null))
                        .collect(Collectors.toList());
            }
            return service.list(queryWrapper.select("cw_id", "name"));
        };
    }

}
